//
//  SingleActionTool.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/27.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SingleActionTool.h"
#import "SZLAccountHelper.h"
#import "SingleListNetWork.h"
#import "SingleActionNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "UIAlertView+Entension.h"
#import "SZLTabBarViewController.h"
#import "WaterRippleWait.h"
#import "SendGoodsSingleView.h"
#import "LottieLoading.h"

@interface SingleActionTool()

//------------退换货-------------//
@property (nonatomic,copy) NSString *company;//快递公司
@property (nonatomic,copy) NSString *single;//快递单号

@end
@implementation SingleActionTool

static SingleActionTool *_tool;

+ (instancetype)shareTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _tool = [[SingleActionTool alloc]init];
    });
    return _tool;
}
//去付款
- (void)singleToPayWithModel:(SingleListModel *)model success:(void (^)(SingleCreateModel *model))successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    [self showWaiter];
    __WEAK
    [SingleListNetWork singleGotoPayWithParame:parame response:^(BOOL success, NSString *msg,SingleCreateModel *model) {
        [weakSelf hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
//确认收货
- (void)sureReceive:(SingleListModel *)model success:(void (^)())successBlock{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认收货吗" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    __WEAK
    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
        if (btnIndex == 1) {
            NSMutableDictionary *parame = [NSMutableDictionary dictionary];
            parame[@"token"] = [SZLAccountHelper shareInstance].token;
            parame[@"orderNo"] = model.orderNo;
            [weakSelf showWaiter];
            [SingleActionNetWork userSureWithParame:parame response:^(BOOL success, NSString *msg) {
                [weakSelf hideWaiter];
                if (success) {
                    if (successBlock) {
                        successBlock();
                    }
                }else{
                    [MBProgressHUD showError:msg];
                }
            }];
        }
    }];
}

//退货申请
- (void)backApply:(SingleListModel *)model success:(void (^)())successBlock
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认发送退货申请吗" message:@"请输入退货理由" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
    __WEAK
    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
        if (btnIndex == 1) {
            UITextField *reasonField = [alert textFieldAtIndex:0];
            NSMutableDictionary *parame = [NSMutableDictionary dictionary];
            parame[@"token"] = [SZLAccountHelper shareInstance].token;
            parame[@"orderNo"] = model.orderNo;
            parame[@"reason"] = reasonField.text;
            [weakSelf showWaiter];
            [SingleActionNetWork userBackApplyWithParame:parame response:^(BOOL success, NSString *msg) {
                [weakSelf hideWaiter];
                if (success) {
                    if (successBlock) {
                        successBlock();
                    }
                }else{
                    [MBProgressHUD showError:msg];
                }
            }];
        }
    }];
}

//退货操作  商家发货（需物流信息）
- (void)sendBack:(SingleListModel *)model type:(BOOL)isSeller success:(void (^)())successBlock
{
    SendGoodsSingleView *sendView =[SendGoodsSingleView loadSendView];
    __WEAK;
    [sendView showWithFrame:[UIScreen mainScreen].bounds success:^(NSString *companyStr, NSString *singleStr) {
        weakSelf.company = companyStr;
        weakSelf.single = singleStr;
        if (isSeller) {
            [weakSelf sellerSendGoodsRequest:model success:successBlock];
        }else{
            [weakSelf sendBackRequest:model success:successBlock];
        }
    }];
}

//商家发货(自提 不需物流信息)操作
- (void)sellerSendGoods:(SingleListModel *)model success:(void (^)())successBlock
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认发货吗" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    __WEAK
    [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
        if (btnIndex == 1) {
            [weakSelf sellerSendGoodsRequest:model success:successBlock];
        }
    }];
//    if (model.isUserselfPick) {
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认发货吗" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        [alert show];
//        __WEAK
//        [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
//            if (btnIndex == 1) {
//                [weakSelf sellerSendGoodsRequest:model success:successBlock];
//            }
//        }];
//    }else{
//        [self sendBack:model type:YES success:successBlock];
//    }
}
//商家同意拒绝退货操作
- (void)sellerAgreeRefuse:(SingleListModel *)model type:(NSInteger)status success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    parame[@"status"] = @(status);
    __WEAK
    [self showWaiter];
    [SingleActionNetWork sellerAgreeRefuseWithParame:parame success:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
//商家确认收到退货操作
- (void)sellerSureReturn:(SingleListModel *)model success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    __WEAK
    [self showWaiter];
    [SingleActionNetWork sellerSureReturnParame:parame success:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
//商家打款
- (void)sellerReturnMoney:(SingleListModel *)model success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    __WEAK
    [self showWaiter];
    [SingleActionNetWork sellerReturnMoneyParame:parame success:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
//用户取消订单
- (void)singleCancel:(SingleListModel *)model success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    __WEAK
    [self showWaiter];
    [SingleActionNetWork userCancelSingleWithParame:parame response:^(BOOL success, NSString *msg) {
        [weakSelf hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock(model);
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
#pragma mark ------------------请求------------------------
//用户退货请求
- (void)sendBackRequest:(SingleListModel *)model success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    parame[@"expressCompany"] = self.company;
    parame[@"courierNumber"] = self.single;
    [self showWaiter];
    [SingleActionNetWork userSendBackWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            if (successBlock) {
                successBlock();
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}
//商家发货请求
- (void)sellerSendGoodsRequest:(SingleListModel *)model success:(void (^)())successBlock
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = model.orderNo;
    parame[@"expressCompany"] = model.expressCompany;
    parame[@"courierNumber"] = model.courierNumber;
//    [MBProgressHUD showMessage:@""];
    [SingleActionNetWork sellerSendGoodsWithParame:parame response:^(BOOL success, NSString *msg) {
//        [MBProgressHUD hideHUD];
        if (success) {
            if (successBlock) {
                successBlock();
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}

- (void)showWaiter
{
//    [WaterRippleWait waterRippleShowBackgroundColor:[UIColor clearColor]];
    [LottieLoading showLoadingInView:nil];
}
- (void)hideWaiter
{
//    [WaterRippleWait hideWaterRipple];
    [LottieLoading hideLoadingInView:nil];
}

@end
